package com.m3958.firstgwt.server.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.openid4java.consumer.ConsumerManager;

import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.inject.Singleton;
import com.m3958.firstgwt.server.service.AppUtilService;
import com.m3958.firstgwt.server.service.IframeResultStoreService;
import com.m3958.firstgwt.server.service.ReqParaService;
import com.m3958.firstgwt.server.types.AppConstants;

@Singleton
public class IframeFormStatusServlet extends HttpServlet{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	@Inject
	private AppUtilService apputils;
	
	@Inject
	private Injector injector;
	
	@Inject
	protected IframeResultStoreService irss;
	
	
	@Inject
	public ConsumerManager manager;
	
	@Override
	public void doGet(HttpServletRequest req, HttpServletResponse res) 
	                               throws ServletException, IOException {
		ReqParaService reqps = injector.getInstance(ReqParaService.class);
		String tid = reqps.getTid();
		String ifs = irss.getIframeResult(tid);
		if(ifs == null){
			apputils.writeJsonpResponse(res,getEmptyIfs(tid).toString(), req.getParameter("callback"));
		}else{
			apputils.writeJsonpResponse(res, ifs, req.getParameter("callback"));
		}
	}
	
	private JSONObject getEmptyIfs(String tid){
		JSONObject jo = apputils.getEmptyListResponse();
		JSONObject m3958jo = new JSONObject();
		m3958jo.element("tid", tid);
		jo.element(AppConstants.M3958EXTRA, m3958jo);
		return jo;
	}
}
